/*
 * =====================================================================================
 *
 *       Filename:  readn.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  10/03/11 20:22:46
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#include "unp.h"

ssize_t readn(int fd, void* vptr, size_t n){
    
    size_t nleft;
    ssize_t nread;
    char *ptr;

    ptr = (char*)vptr;
    nleft = n;

    while(nleft > 0){
        if((nread = read(fd,ptr,nleft)<0)){
            if(errno == EINTR)
                nread = 0;
            else
                return (-1);
        }else if(nread == 0){
            break;
        }

        nleft -=nread;
        ptr   +=nread;
    }
    return (n-nleft);
}
